Select Case and ComboBox |
Objectives |
Discussion |
Select...Case
Select Case sState
case "PA"
sMessage = "Pennsylvania"
case "MD"
sMessage = "Maryland"
case "VA"
sMessage = "Virginia"
End Select
Select Case iX
case 2
messagebox.show("even")
case 3, 5
messagebox.show("odd")
case 6 to 10
messagebox.show("Between 6 and 10")
case Is > 10
messagebox.show("Greater than 10")
case Else
messagebox.show("No Clue")
End Select
ComboBox
Demonstration |
1. Add a form to your Unit3 project.
2. We suggest that you add a button to your form to exit your program. To make the button end your program, just type "End" in the click event.
3. Add a combobox (cboStatus ) to the form. We are going to add items to the combobox so that the user can change the type of border of the form. In the properties window for the combobox select Items and then click on the button next to the word "collection". A window should appear. Just type the following into the window. These will be the items in the combobox:
None
Fixed Single
Fixed 3D
Sizable
4. Now double-click the combobox to open the SelectedIndexChanged event. This event is fired when the user clicks on an item in the combobox. We are going to add code to change the forms' BorderStyle which is a property of the form. You can also change this property in the properties window. Type the following into the event.
Select Case cboStatus.SelectedItem
Case "None"
Me.FormBorderStyle = FormBorderStyle.None
Case "Fixed 3D"
Me.FormBorderStyle = FormBorderStyle.Fixed3D
Case "Fixed Single"
Me.FormBorderStyle = FormBorderStyle.FixedSingle
Case "Sizable"
Me.FormBorderStyle = FormBorderStyle.Sizable
End Select
5. You should type at least one of the lines "Me.FormBorderStyle = " and notice that VS provides a list of possible options.
6. Run the program and test it out.
Exercises |
1. Create a function that accepts a string that contains the two-digit code for a state (MD,PA, DE,VA...) and returns the name of the state. Make sure that your function handles at least four different states and uses the Select...Case statement. Make sure that the function can handle the situation in which the code passed into the function does not match one of the four cases.
2. Create a program to use your function. Use a combobox to list the four codes. When the user selects an item in the combobox, the function from #1 should be called and the state name displayed in a messagebox with the message "You selected [the state name here]".
3. Modify your program by adding a textbox and button. When the user clicks on the button, the text in the textbox is added to the combobox.
4. Write a function that accepts an Integer representing someones age and then returns a string indicating the age category for the person:
Age | Category |
0-5 | Toddler |
6-12 | Juvenile |
13 - 19 | Teenager |
20 - 29 | Twenties |
30 - 39 | Thirties |
> 40 | Over 40 |
Use a Select...Case statement.
5. Write a program that uses the function from #4. The form should have a textbox and a button. When the user clicks the button, the number in the textbox should be checked to determine the category and then display the result in a messagebox.
6. What other type of statement can you use instead of a Select...Case statement?
Links & Help |